package com.sptpc.learnspring.service.impl;

import com.sptpc.learnspring.mapper.UserMapper;
import com.sptpc.learnspring.model.User;
import com.sptpc.learnspring.security.JwtUser;
import com.sptpc.learnspring.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        User user = userMapper.selectByUsername(username);
        UserDetails userDetails = new JwtUser(user);
        return userDetails;
    }

    @Override
    public boolean register(User user) {
        int flag = userMapper.insert(user);
        return flag==1?true:false;
    }
}
